package com.baseframe.oss.template;

import com.baseframe.oss.constant.BaseOssConstant;
import com.baseframe.oss.entity.Oss;
import com.baseframe.oss.entity.OssFile;
import com.baseframe.oss.props.BaseOssProperties;
import com.baseframe.tool.utils.data.EnvUtil;
import com.baseframe.tool.utils.encrypt.Md5Util;
import com.baseframe.tool.utils.spring.SpringUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;


import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@AllArgsConstructor
@Getter
@Setter
public abstract class AbstractOssTemplate implements OssTemplate{


    private final Oss oss;


    /**
     * 获取文件大小
     * @param inputStream 字节输入流
     * @return 文件大小 单位kb
     */
    public  int getFileSize(InputStream inputStream){
        int available =0;
        try {
            available = inputStream.available();
            if (available<1024){
                return 1;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return available/1024;
    }


    /**
     * 获取文件要存储到的文件夹
     * @return
     */
    public String getUploadPathEnv(){
        BaseOssProperties ossProperties = SpringUtil.getBean(BaseOssProperties.class);
        String uploadPath = ossProperties.getUploadPath();
        if (uploadPath==null){
            uploadPath = BaseOssConstant.DEFAULT_UPLOAD_PATH;
        }
        //解析规则
        String[] envStrArr = EnvUtil.getEnvStr(uploadPath);
        SimpleDateFormat sdf = new SimpleDateFormat();
        List<String> argsList = new ArrayList<>();
        for (String envStr : envStrArr) {
            BaseOssConstant.PathRule pathRule = BaseOssConstant.PathRule.getRole(envStr);
            sdf.applyPattern(pathRule.getPattern());
            String dateStr= sdf.format(new Date());
            argsList.add(dateStr);

        }


        return EnvUtil.parsingStr(uploadPath, (Object[]) argsList.toArray(new String[0]));
    }

    /**
     * 获取文件唯一标识
     * @param fileName 加密后的文件名
     * @return
     */
    public String getFileKey(String fileName){


        String uploadPathEnv = getUploadPathEnv();
        String pathEnv = uploadPathEnv.substring(1);//去掉 /xxx/{xxx} 最前面的一个 /
        return pathEnv.concat("/").concat(fileName);
    }

    /**
     * 获取加密之后的文件名
     * @param originalName
     * @return
     */
    public  String getFileName(String originalName){

        String[] split = originalName.split("\\.");
        //后缀
        String suffix = "";
        StringBuilder nameSb = new StringBuilder();
        if (split.length>=2){
            suffix = split[split.length-1];
            for (int i = 0; i < split.length - 1; i++) {
                nameSb.append(split[i]);
            }
        }else {
            nameSb.append(split[0]);
        }
        nameSb.append(new Date().getTime());
        //md5加密
        String encode = Md5Util.encode(nameSb.toString());


        return split.length>=2?encode+"."+suffix:encode;
    }

    /**
     * 上传文件
     * @param originalName 源文件名
     * @param inputStream 输入流
     * @return ossFile文件对象
     */
    public abstract OssFile putFile(String originalName, InputStream inputStream);

    /**
     * 删除文件
     * @param fileKey 文件标识
     * @return bool 是否成功
     */
    public abstract boolean DeleteFile(String fileKey);

    /**
     * 获取文件访问链接
     * @param fileKey 文件唯一标识
     * @return 文件链接 link
     */
    public abstract String getFileLink(String fileKey);


    public static void main(String[] args) {
        System.out.println(BaseOssConstant.DEFAULT_UPLOAD_PATH.substring(1));
    }








}
